﻿using Aspose.Words;
using ERPSystem.Common.Domains.SalesOrderCommom;
using ERPSystem.Common.DTO;
using ERPSystem.Common.DTO.ShippingnoteDTO;
using ERPSystem.Common.Infrastructure.IRepository;
using MediatR;
using NPOI.SS.Formula.Functions;
using SqlSugar;
using System.Xml.Linq;

namespace SalesManagementCommon.API.SalesManagementCommons.Orderdetails
{
    public class OrderdetailsHandler : IRequestHandler<OrderdetailsCommand, Result<List<OrderdetailsDTO>>>
    {
        private readonly IBaseRepository<SalesOrder> salesOrderRepository;
        private readonly IBaseRepository<SalesOrderMaterial> salesOrderMaterialRepository;

        public OrderdetailsHandler(IBaseRepository<SalesOrder> salesOrderRepository, IBaseRepository<SalesOrderMaterial> salesOrderMaterialRepository)
        {
            this.salesOrderRepository = salesOrderRepository;
            this.salesOrderMaterialRepository = salesOrderMaterialRepository;
        }




        /// <summary>
        /// 销售订单明细数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<List<OrderdetailsDTO>>> Handle(OrderdetailsCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<List<OrderdetailsDTO>>();

            var salesOrder = await (await salesOrderRepository.GetAll()).ToListAsync();
            var salesOrderMaterial = await (await salesOrderMaterialRepository.GetAll()).ToListAsync();
            var list = (from a in salesOrder
                        join b in salesOrderMaterial on a.ItemBianma equals b.ItemBianma
                        select new OrderdetailsDTO()
                        {
                            SalesOrderId = a.SalesOrderId,
                            SalesStaff = a.SalesStaff,
                            DocumentNumber = a.DocumentNumber,
                            BusinessType = a.BusinessType,
                            DeliverDate = a.DeliverDate,
                            ItemBianma = a.ItemBianma,
                            MaterialName = b.MaterialName,
                            Quantity = b.Quantity,
                            Quality = b.Quality,
                            Packing = b.Packing,
                            Numberofpackages = b.Numberofpackages,
                            DestinationCountry = a.DestinationCountry

                        }).ToList();

            result.Data = list;
            return result;
        }

    }
}
